package com.like.banzipao.server.util.roomstate.playroomstate

import com.like.banzipao.server.model.FriendType
import com.like.banzipao.server.model.Player
import com.like.banzipao.server.model.PlayerState
import com.like.banzipao.server.model.Poker
import com.like.banzipao.server.model.PokerState
import com.like.banzipao.server.model.Room
import com.like.banzipao.server.util.roomstate.BaoType

/**
 * 爆牌 1 vs 3 对决模式
 */
class OneVsThreeRoomState(room: Room, private val baoType: BaoType, private val onWin: () -> Unit) : PlayRoomState(room) {

    override fun onPlayPlayed(player: Player, nextPlayer: Player, curPlayedPokers: List<Poker>): Boolean {
        player.state = PlayerState.PLAY_PLAYED
        room.players.forEach {
            if (it?.user?.id != player.user.id) {
                it?.state = PlayerState.PLAYING
            }
        }
        curPlayedPokers.forEach {
            it.state = PokerState.PLAY_PLAYED
        }
        if (!checkWin(player)) {
            player.nextValidPlayerTurn(PlayerState.PLAY_TURN)
            prePlayedPokers.clear()
            prePlayedPokers.addAll(curPlayedPokers)
        } else {
            onWin()
        }
        return true
    }

    override fun onPlayPass(player: Player, nextPlayer: Player) {
        player.state = PlayerState.PLAY_PASS
        // 如果3家pass，则本轮胜出
        if (room.players.count { it?.state == PlayerState.PLAY_PASS } == 3) {
            prePlayedPokers.clear()
            player.nextValidPlayerTurn(PlayerState.PLAY_TURN_WIN)
        } else {
            player.nextValidPlayerTurn(PlayerState.PLAY_TURN)
        }
    }

    override fun getDealerUserId(): Int {
        return room.players.find {
            it?.friendTypeTemp == FriendType.BAO
        }?.user?.id ?: -1
    }

    override fun getIntegralChange(player: Player): Int {
        return if (player.state == PlayerState.PLAY_WIN) {
            if (baoType == BaoType.BAO) 12 else 9
        } else {
            if (baoType == BaoType.BAO) -4 else -3
        }
    }

    private fun checkWin(player: Player): Boolean {
        // 任意一家出完本局就结束了。
        if (player.isAllPokersPlayed()) {
            room.players.forEach {
                it?.state = if (it?.friendTypeTemp == player.friendTypeTemp) {
                    PlayerState.PLAY_WIN
                } else {
                    PlayerState.PLAY_LOSE
                }
            }
            if (baoType == BaoType.BAO) {
                room.players.forEach {
                    if (it?.state == PlayerState.PLAY_WIN) {
                        it.integral += 12
                    } else {
                        it!!.integral -= 4
                    }
                }
            } else if (baoType == BaoType.DRAW_BAO) {
                room.players.forEach {
                    if (it?.state == PlayerState.PLAY_WIN) {
                        it.integral += 9
                    } else {
                        it!!.integral -= 3
                    }
                }
            }
            return true
        }
        return false
    }

}